﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.DAO;
using StoreTransport.Business.Constants;
using StoreTransport.Resources;
using StoreTransport.Business.Helper;
using StoreTransport.Data;



namespace StoreTransport.Presentation.Forms {
    public partial class RegionListForm : Form {
        int id = 0;
        Dictionary<string, string> asmDic = new Dictionary<string,string>();
        public RegionListForm() {
            InitializeComponent();
        }
        private void RegionList_Load(object sender, EventArgs e) {
            _tbxRegionName.Select();
            _drpASMList.DisplayMember = ColumnName.ASMName;
            _drpASMList.ValueMember = ColumnName.ID;
            StoreTransportDataContext context = CommonHelper.GetContext();
            _drpASMList.DataSource = context.ASMs.OrderBy(c => c.ASMName);
            asmDic = context.ASMs.ToDictionary(c => c.ID.ToString(), c => c.ASMName);
            LoadRegions();
        }
        private void LoadRegions() {
            _grdRegions.AutoGenerateColumns = false;
            var regionList = CommonHelper.GetContext().Regions.
                Cast<StoreTransport.DAO.Region>().OrderBy(c => c.RegionName);
            List<PresentRegionDTO> prList = new List<PresentRegionDTO>();
            foreach (StoreTransport.DAO.Region r in regionList) {
                string asm = string.Empty;
                if (r.ASM != null && asmDic.ContainsKey(r.ASM.ToString())) {
                    asm = asmDic[r.ASM.ToString()];
                }
                prList.Add(new PresentRegionDTO(r.ID, r.RegionName, asm));
            }
            _grdRegions.DataSource = prList.OrderBy(c => c.RegionName).ToList();

        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            _tbxRegionName.Select();
            _tbxRegionName.Text = string.Empty;
            _lblID.Text = StoreTransportResource.Form_NewID;
            id = 0;
            CommonHelper.ResetCombobox(_drpASMList);
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            DAO.Region re = null;
            string errorMessage = CommonHelper.CheckRequiredField(_tbxRegionName.Text, _lblRegionName.Text);
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage);
                return;
            }
            StoreTransportDataContext context = CommonHelper.GetContext();
            if (id == 0) {
                if (context.Regions.FirstOrDefault(c => c.RegionName.Equals(_tbxRegionName.Text)) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_Region, _tbxRegionName.Text));
                    return;
                }
                re = new DAO.Region();
                context.Regions.InsertOnSubmit(re);
            } else {
                var item = context.Regions.FirstOrDefault(c => c.RegionName.Equals(_tbxRegionName.Text));
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_Region, _tbxRegionName.Text));
                    return;
                }
                re = context.Regions.Where(c => c.ID.Equals(id)).First();
            }
            re.ASM = (int)_drpASMList.SelectedValue;
            re.RegionName = _tbxRegionName.Text;
            context.SubmitChanges();
            LoadRegions();
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdRegions.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                    StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    try {
                        StoreTransportDataContext context = CommonHelper.GetContext();
                        foreach (DataGridViewRow row in _grdRegions.SelectedRows) {
                            id = int.Parse(row.Cells[0].Value.ToString());
                            DAO.Region selectedItem = context.Regions.Where(c => c.ID.Equals(id)).First();
                            context.Regions.DeleteOnSubmit(selectedItem);
                            context.SubmitChanges();
                        }
                        LoadRegions();
                        id = 0;
                    } catch (Exception ex) {
                        MessageBox.Show(StoreTransportResource.ErrorMessage_CanNotDeleteItem);
                    }
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdRegions.SelectedRows.Count > 0) {
                id = int.Parse(_grdRegions.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            DAO.Region selectedItem = CommonHelper.GetContext().Regions.Where(c => c.ID.Equals(id)).First();
            _tbxRegionName.Text = selectedItem.RegionName;
            if (selectedItem.ASM != null) {
                _drpASMList.SelectedValue = selectedItem.ASM;
            } else {
                CommonHelper.ResetCombobox(_drpASMList);
            }
            _lblID.Text = id.ToString();
        }

        private void _grdRegions_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdRegions.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }
    }
}
